<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                attachlinkRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#attachlinkGird").jqGrid('getGridParam', 'selrow');
                        var attachlink = jQuery("#attachlinkGird").jqGrid('getRowData', rowid);
                        dijit.byId('attachlinkUFD').setValues(attachlink);
                    }
                };
                topic.subscribe("attachlink/insertOrUpdate", attachlinkRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#attachlinkGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#attachlinkGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#attachlinkGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#attachlinkGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                attachlinkSubmitForm = function(formId, type) {
                    var url = "attachlink/insert";
                    if (type == 'u') {
                        url = "attachlink/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("attachlink/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                attachlinkDelete = function() {
                    var ids = jQuery("#attachlinkGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var attachlink = {};
                            attachlink.id = '';
                            array.forEach(ids, function(entry, i) {
                                attachlink.id += entry + ",";
                            });
                            syncDB("attachlink/delete", JSON.stringify(attachlink));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                attachlinkQuery = function(formId) {
                    var attachlink = dijit.byId(formId).getValues();
                    jQuery("#attachlinkGird").jqGrid('setGridParam', {
                        postData : attachlink,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="attachlinkIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="entityid">实体ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="entityid"></td>
      </tr>
      <tr>
        <td><label for="fileid">附件ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="fileid"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="attachlinkSubmitForm('attachlinkIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="attachlinkRestForm('attachlinkIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="attachlinkUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="entityid">实体ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="entityid"></td>
      </tr>
      <tr>
        <td><label for="fileid">附件ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="fileid"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="attachlinkSubmitForm('attachlinkUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="attachlinkRestForm('attachlinkUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryAttachLinkForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="entityid">实体ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="entityid"></td>
        </tr>
        <tr>
          <td><label for="fileid">附件ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="fileid"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="attachlinkInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('attachlinkIFD').show();
      </script>
    </button>
    <button id="attachlinkDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="attachlinkDelete();"></button>
    <button id="attachlinkUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#attachlinkGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var attachlink = jQuery("#attachlinkGird").jqGrid('getRowData', ids[0]);
          dijit.byId('attachlinkUFD').setValues(attachlink);
          dijit.byId('attachlinkUFD').show();
        }
      </script>
    </button>
    <button id="attachlinkQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="attachlinkQuery('queryAttachLinkForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="attachlinkGird"></table>
  <div id="attachlinkToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#attachlinkGird").jqGrid({
        url : 'attachlink/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '实体ID',
          '附件ID'
        ],
        colModel : [ 
        {
          name : 'entityid',
          index : 'entityid'
        },
        {
          name : 'fileid',
          index : 'fileid'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#attachlinkToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "附件关联表列表"
    });
    jQuery("#attachlinkGird").jqGrid('navGrid', '#attachlinkToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
